/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of Symbol Matching.
 *
 * Symbol Matching is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * Symbol Matching is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Symbol Matching.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.reaktiopeli.game;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import fi.honkalampisaatio.reaktiopeli.ReaktioPeli;

public class QuestionPanel extends JPanel {

	private static final long serialVersionUID = 2336604328259818964L;

	private String instruction;
	private BufferedImage questionImg;
	private String questionStr;
	private String questionInstruction;

	private Font questionsFont;

	private Color questionsFontColor;

	public QuestionPanel()
	{
		questionImg = null;
		instruction = null;
		questionStr = null;
		questionInstruction = null;
	}
	
	/**
	 * Paints on screen
	 */
	public void paintComponent(Graphics g) {
        super.paintComponent(g);   
    	int questionImgBottom;
        String inst;   
        Graphics2D g2 = (Graphics2D) g;
        Color primColor = g2.getColor();
    	g2.setFont(getFont().deriveFont((float) (60)));
    	if(this.questionInstruction != null)
    	{
    		inst = questionInstruction;
    	}
    	else
    	{
    		inst = this.instruction;
    	}
    	
        if(inst!=null ) {
    		Rectangle2D rc = null;
    		float fontSize = 144;
    		int maxHeight = (int) (this.getHeight()*0.15f);
    		int maxWidth = (int) (this.getWidth()*0.9f);
        	g2.setFont(getFont().deriveFont(fontSize));
        	if(g2.getFontMetrics().getHeight()!=0) {
        		fontSize = (int)(fontSize * maxHeight/g2.getFontMetrics().getHeight());
            	g2.setFont(getFont().deriveFont(fontSize));
        	}
        	rc = g2.getFontMetrics().getStringBounds(inst, g2);
        	if(rc.getWidth()!=0 && rc.getWidth()>maxWidth) {
        		fontSize = (float) (fontSize * maxWidth/rc.getWidth());
            	g2.setFont(getFont().deriveFont(fontSize));
            	rc = g2.getFontMetrics().getStringBounds(inst, g2);
        	}
        	int y = (int) (g2.getFontMetrics().getMaxAscent() + (maxHeight-rc.getHeight())/2);
        	int x = (int)((this.getWidth()-rc.getWidth())/2);
        	g2.drawString(inst, x, y);

        	if(ReaktioPeli.debugUI)
        	{
            	g2.setColor(Color.BLUE);
            	g2.drawRect(2, 2, this.getWidth()-4, maxHeight-4);
            	g2.setColor(primColor);
        	}
        }
    	//image
    	if(questionImg!=null) {
    		int height, width;
    		float scale = 1;
    		if(questionStr!=null)
    		{
    			height = (int)(this.getHeight()*0.7);
    		}
    		else
    		{
    			height = (int)(this.getHeight()*0.85);
    		}
    		width = questionImg.getWidth()*height/questionImg.getHeight();
    		if(width > this.getWidth()) {
    			scale = ((float)this.getWidth())/width;
    			//width = (int)(width*scale);
    			//height = (int)(height*scale);
    		}
    		g2.drawImage(questionImg, (this.getWidth()-(int)(width*scale))/2, 
    				(int)(this.getHeight()*0.15 + (height - height*scale)/2), 
    				(int)(width*scale), (int)(height*scale), null);
    		questionImgBottom = (int)(this.getHeight()*0.15 + (height - height*scale)/2 + height*scale);

        	if(ReaktioPeli.debugUI)
        	{
        		g2.setColor(Color.CYAN);
            	g2.drawRect((this.getWidth()-(int)(width*scale))/2, 
            			(int)(this.getHeight()*0.15 + (height - height*scale)/2), 
        				(int)(width*scale), (int)(height*scale));
        		g2.setColor(Color.GREEN);
            	g2.drawRect(2, (int)(this.getHeight()*0.15), this.getWidth()-4, (int)(height-4));
            	g2.setColor(primColor);
        	}
  
    	}
    	else
    	{
    		questionImgBottom = (int)(this.getHeight()*0.15);
    	}
    	if(questionStr!=null) {
    		Font font = null;
    		Rectangle2D rc = null;
    		double maxWidth = this.getWidth()*0.9f;
    		double maxHeight = this.getHeight() - questionImgBottom;
    		float fontSize = 144;

    		if(this.questionsFont!=null)
    			font = this.questionsFont;
    		else
    			font = getFont();
    		if(this.questionsFontColor!=null)
    			g2.setColor(this.questionsFontColor);
    		
        	g2.setFont(font.deriveFont(fontSize));
        	
        	rc = g2.getFontMetrics().getStringBounds(this.questionStr, g2);
    		fontSize = (float) (fontSize*maxHeight/rc.getHeight());
    		g2.setFont(font.deriveFont(fontSize));
        	
        	rc = g2.getFontMetrics().getStringBounds(this.questionStr, g2);
        	if(rc.getWidth()>maxWidth) {
        		fontSize = (float)(fontSize*maxWidth/rc.getWidth());
        		g2.setFont(font.deriveFont(fontSize));
        	}

        	rc = g2.getFontMetrics().getStringBounds(this.questionStr, g2);
        	g2.drawString(this.questionStr, (int)((this.getWidth()-rc.getWidth())/2), 
        			(int)((maxHeight+rc.getHeight())/2 + questionImgBottom
        					- g2.getFontMetrics().getDescent()));

    		g2.setFont(getFont().deriveFont(fontSize));
        	g2.setColor(primColor);
        	if(ReaktioPeli.debugUI)
        	{
            	g2.setColor(Color.RED);
            	g2.drawRect((int)(this.getWidth()-maxWidth)/2, questionImgBottom, 
            			(int)maxWidth, (int)maxHeight-1);
            	g2.setColor(primColor);
        	}
  
    	}
	}
	
	public void setInstruction(String instruction)
	{
		this.instruction = instruction;
		this.repaint();
	}

	/*
	private BufferedImage questionImg;
	private String questionStr;
	private String questionInstruction;
	*/
	public void setQuestion(MediaElement me)
	{
		if(me!=null) {
			this.questionImg = me.getImage();
			this.questionStr = me.getText();
			this.questionInstruction = me.getInstruction();
		}
		else {
			this.questionImg = null;
			this.questionStr = null;
			this.questionInstruction = null;
		}
		this.repaint();
	}

	public void setQuestionsFont(Font font) {
		this.questionsFont = font;
	}

	public void setQuestionsFontColor(Color color) {
		this.questionsFontColor = color;
	}

}
